<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div>
                <header style="height: 100%">
                    <div align="left">
	                    <table style="width: 100%">
	                   		<tr>
	                   			<td>
		                   			<form class="form-inline" id='formSearchAddBt' onsubmit="return false" action="/pPurchOrderItemss/itemQueryAdd" method="post">
                                        <div>
                                          <input type="hidden" id="orderId" name="orderId" />
                                          供应商：<span id=supplier_name> </span> &nbsp;&nbsp;  采购单号：<span id=orderIdSpan> </span>  &nbsp;&nbsp;  采购状态：<span id=orderStatusSpan> </span>
                                          <br>
                                          
                                          采购日期：<span id=buydateSpan> </span>   &nbsp;&nbsp;  预估商品总金额<span id=pre_goods_amountSpan> </span>   &nbsp;&nbsp;  预估总金额 <span id=pre_amountSpan > </span>  
                                          
                                        </div>
										<div class="form-group">
											
										</div>
									</form>
	                   			</td>
	                   			<td align="right">
                                    <button class="layui-btn layui-btn-sm" onclick="saveOrderItems(0)">
                                       保存
                                    </button>

	                   				<button class="layui-btn layui-btn-sm" onclick="saveOrderItems(1)">
                                       确认提交
                                    </button>

                                    <button class="layui-btn layui-btn-sm" onclick="location.href='pPurchOrdersList.html'">
                                       返回
                                    </button>

	                   			</td>
	                   		</tr> 
	                    </table>
					</div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
									<th>id</th>
									<th>内部商品编号</th>
									<th>商品名称</th>
									<th>库存编号</th>
									<th>采购量</th>
									<th>验货量</th>
									<th>预计商品采购价</th>
                                    <th>创建时间</th>
                                    <th>更新时间</th>
                                    <th>操作</th>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript">
var pers = checkPermission();

var example;
function init(){
    var orderId = getUrlParam("id");
	example = 
    	$('#dt-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	"language": {
                "url": "/js/plugin/datatables/Chinese.lang"
            },
        	"ajax": {
        		"url" : "/pPurchOrderItemss/list",
                "type":"get",
                "data":function(d){
                    d.orderId= orderId;
                    d.id = $("#id").val();
                }
        	},
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
				{"data" : "id", "defaultContent" : ""},
				{"data" : "itemNo", "defaultContent" : ""},
				{"data" : "goodsName", "defaultContent" : ""},
				{"data" : "sku", "defaultContent" : ""},
				{"data" : "qty", "defaultContent" : "","orderable":false},
                {"data" : "receiveQty", "defaultContent" : "","orderable":false,
                    "render": function (data, type, row) {
                                   var id = row['id'];
                                   var html=  "<input type=text name=receive_qty["+ row['id'] +"] value="+ row['receiveQty'] + " data_id="+row['id']+" class='layui-input item_receive_qtys'  />";
                                   return html;
                                }

                },
				{"data" : "preBuyPrice", "defaultContent" : ""},
				{"data" : "createTime", "defaultContent" : ""},
				{"data" : "updateTime", "defaultContent" : ""},
				{ 
								"data": "", 
								"defaultContent": "",
								"orderable":false,
						        "render": function (data, type, row) {
                                   return "";
                  	            }		
							},
                
            ],
           "order": [[ 0, "asc" ]]
        } );
	
	if(orderId != ""){
        $.ajax({
            type : 'get',
            url : '/pPurchOrderss/'+orderId,
            async : false,
            success : function(data) {
                $('#orderIdSpan').html(data.id);
                $('#buydateSpan').html(data.buydate);
                $('#supplier_name').html(data.supplierNo + "  "+data.supplierName);
                $('#orderStatusSpan').html(data.status + "  "+data.statusTitle );
                $('#pre_goods_amountSpan').html("  "+data.preGoodsAmount);
                $('#pre_amountSpan').html("  "+data.preAmount);
                
                /*
                $('#type').val(data.type);
                $('#status').val(data.status);
                $('#preGoodsAmount').val(data.preGoodsAmount);
                $('#preExpenses').val(data.preExpenses);
                $('#preAmount').val(data.preAmount);
                $('#cancelAmount').val(data.cancelAmount);
                $('#amount').val(data.amount);
                */

            }
        });
    }
	
}

layui.use('layer', function(){
    var layer = layui.layer;
});

$("#searchBt").click(function(){
	example.ajax.reload();
});

$("#searchAddBt").click(function(){
     var orderId = getUrlParam("id");
     var d={
        "orderId":orderId,
        "itemNo":$("#formSearchAddBt").find("[name=itemNo]").val(),
        "sku":$("#formSearchAddBt").find("[name=sku]").val(),
        "qty":$("#formSearchAddBt").find("[name=qty]").val(),
     }
     $.ajax({
            type : 'post',
            url : '/pPurchOrderItemss/itemQueryAdd',
            data : d,
            success : function(data) {
                if(data !=null && data.message){
                    layer.msg(data.message);
                }
                if(data.code==0){
                    example.ajax.reload();
                }
            }
        });
});

function saveOrderItems(type){
	debugger;
    var id = getUrlParam("id");
    var d={
        'orderId':id,
        'type':type,
        'receive_qtys': JSON.stringify(elementArray2Json($(".item_receive_qtys")))
     };
     $.ajax({
        type : 'post',
        url : '/pPurchOrderItemss/receive',
        data : d,
        success : function(data) {
            if(type==1){
                layer.msg(data.message, {shift: -1, time: 1000}, function(){
                        location.href = "pPurchOrdersList.html";
                    });
            }else{
                layer.msg(data.message);
            }
        }
    });
}


init();
</script>
